<template>
	<div>
		<div class="padding">
			<el-descriptions :title="nowRowData.organizationName">
				<el-descriptions-item>
					<span>本月第 {{ formModel.checkTotal }} 次抽查</span>
				</el-descriptions-item>
				<el-descriptions-item label="地址" :span="2">
					{{ formModel.areanNames }}
				</el-descriptions-item>
				<el-descriptions-item label="摄像头编号">
					{{ nowRowData.videoNum }}
				</el-descriptions-item>
				<el-descriptions-item label="位置" class-name="base-item">
						<DictTags :value="nowRowData.videoLocation" code="VIDEO_LOCATION"></DictTags>
				</el-descriptions-item>
			</el-descriptions>
		</div>
		<AplidForm ref="formRef" v-bind="spotCheckForm" v-if="!detail" v-model="formModel" @submit="onSubmit" @reset="onReset"> </AplidForm>
		<DetailPage :model="nowRowData" v-else :nowRowData="nowRowData" v-bind="spotCheckForm" :showBtn="false"></DetailPage>
	</div>
</template>

<script setup lang="ts">
import { spotCheckForm } from '../config'
import apiMod from '@/apiMod/index'
import { ElMessage } from 'element-plus'
import DetailPage from '@/views/aplid/DetailPage/index.vue'
import DictTags from '@/views/aplid/DetailPage/ContentListItem/DictTags/home'
import { getAreaObj } from '@/utils/util'

const props = withDefaults(defineProps<{ nowRowData: any; detail: boolean }>(), {
	detail: false,
})

const emit = defineEmits(['refresh'])

const formModel = ref(props.nowRowData)

const onReset = () => {
	emit('refresh')
}

const onSubmit = (form, done) => {
	const query = {
		...props.nowRowData,
		...form,
		rectificationStatus: 1,
	}
	apiMod.view_scSpotCheckMonitor_edit(query).then(() => {
		ElMessage.success('操作成功')
		onReset()
		done()
	})
}
const getMonthLastDateFn = (year, month) => {
	// let dateStr = '2020-02-06'; //需要获取此月最后一天的日期
	let dateObj = new Date(year + '-' + month + '-01')
	let nextMonth = dateObj.getMonth() + 1 //0-11，下一个月
	//设置当前日期为下个月的1号
	dateObj.setMonth(nextMonth)
	dateObj.setDate(1) //1-31

	let nextMonthFirstDayTime = dateObj.getTime() //下个月一号对应毫秒

	let theMonthLastDayTime = nextMonthFirstDayTime - 24 * 60 * 60 * 1000 //下个月一号减去一天，正好是这个月最后一天

	let theMonthDay = new Date(theMonthLastDayTime).getDate()
	return year + '-' + month + '-' + theMonthDay
}
onMounted(() => {
	const areas: any = getAreaObj(props.nowRowData, '', 'get')
	formModel.value.areanNames = areas.names

	const d = new Date()
	const year = d.getFullYear()
	const m = d.getMonth() + 1
	const mm = m < 10 ? '0' + m : m
	const scTimeBetween = `${year}-${mm}-01 00:00:00,${getMonthLastDateFn(year, mm)} 23:59:59`
	apiMod
		.view_scSpotCheckMonitor_page({
			creditCode: props.nowRowData.creditCode,
			scTimeBetween,
		})
		.then((res) => {
			const data = res.data
			formModel.value.checkTotal = data.total
		})
})
</script>

<style scoped>
:deep(.base-item) {
  display: inline-block
}

</style>
